home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / demo / qthumbwheel.cpp.z / qthumbwheel.cpp
C/C++ Source or Header  |  2002-04-08  |  6KB  |  299 lines

  1. /****************************************************************************
  2. ** $Id:  qt/qthumbwheel.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Definition of QThumbWheel class
  5. **
  6. ** Created : 010205
  7. **
  8. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  9. **
  10. ** This file is part of the widgets module of the Qt GUI Toolkit.
  11. **
  12. ** This file may be distributed under the terms of the Q Public License
  13. ** as defined by Trolltech AS of Norway and appearing in the file
  14. ** LICENSE.QPL included in the packaging of this file.
  15. **
  16. ** This file may be distributed and/or modified under the terms of the
  17. ** GNU General Public License version 2 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.GPL included in the
  19. ** packaging of this file.
  20. **
  21. ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
  22. ** licenses may use this file in accordance with the Qt Commercial License
  23. ** Agreement provided with the Software.
  24. **
  25. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  26. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. **
  28. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  29. **   information about Qt Commercial License Agreements.
  30. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  31. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  32. **
  33. ** Contact info@trolltech.com if any conditions of this licensing are
  34. ** not clear to you.
  35. **
  36. **********************************************************************/
  37.  
  38. #include "qthumbwheel.h"
  39.  
  40. #ifndef QT_NO_THUMBWHEEL
  41. #include <qpainter.h>
  42. #include <qdrawutil.h>
  43. #include <qpixmap.h>
  44. #include <math.h>
  45.  
  46. static const double m_pi = 3.14159265358979323846;
  47. static const double rad_factor = 180.0 / m_pi;
  48.  
  49. QThumbWheel::QThumbWheel( QWidget *parent, const char *name )
  50.     : QFrame( parent, name )
  51. {
  52.     orient = Vertical;
  53.     init();
  54. }
  55.  
  56. /*!
  57.   Destructs the wheel.
  58. */
  59.  
  60. QThumbWheel::~QThumbWheel()
  61. {
  62. }
  63.  
  64. /*!
  65.   \internal
  66.  */
  67.  
  68. void QThumbWheel::init()
  69. {
  70.     track = TRUE;
  71.     mousePressed = FALSE;
  72.     pressedAt = -1;
  73.     rat = 1.0;
  74.     setFrameStyle( WinPanel | Sunken );
  75.     setMargin( 2 );
  76.     setFocusPolicy( WheelFocus );
  77. }
  78.  
  79. void QThumbWheel::setOrientation( Orientation orientation )
  80. {
  81.     orient = orientation;
  82.     update();
  83. }
  84.  
  85. void QThumbWheel::setTracking( bool enable )
  86. {
  87.     track = enable;
  88. }
  89.  
  90. void QThumbWheel::setTransmissionRatio( double r )
  91. {
  92.     rat = r;
  93. }
  94.  
  95. /*!
  96.   Makes QRangeControl::setValue() available as a slot.
  97. */
  98.  
  99. void QThumbWheel::setValue( int value )
  100. {
  101.     QRangeControl::setValue( value );
  102. }
  103.  
  104. void QThumbWheel::valueChange()
  105. {
  106.     repaint( FALSE );
  107.     emit valueChanged(value());
  108. }
  109.  
  110. void QThumbWheel::rangeChange()
  111. {
  112. }
  113.  
  114. void QThumbWheel::stepChange()
  115. {
  116. }
  117.  
  118. /*!
  119.   \reimp
  120. */
  121.  
  122. void QThumbWheel::keyPressEvent( QKeyEvent *e )
  123. {
  124.     switch ( e->key() ) {
  125.     case Key_Left:
  126.     if ( orient == Horizontal )
  127.         subtractLine();
  128.     break;
  129.     case Key_Right:
  130.     if ( orient == Horizontal )
  131.         addLine();
  132.     break;
  133.     case Key_Up:
  134.     if ( orient == Vertical )
  135.         subtractLine();
  136.     break;
  137.     case Key_Down:
  138.     if ( orient == Vertical )
  139.         addLine();
  140.     break;
  141.     case Key_PageUp:
  142.     subtractPage();
  143.     break;
  144.     case Key_PageDown:
  145.     addPage();
  146.     break;
  147.     case Key_Home:
  148.     setValue( minValue() );
  149.     break;
  150.     case Key_End:
  151.     setValue( maxValue() );
  152.     break;
  153.     default:
  154.     e->ignore();
  155.     return;
  156.     };
  157. }
  158.  
  159. /*!
  160.   \reimp
  161. */
  162.  
  163. void QThumbWheel::mousePressEvent( QMouseEvent *e )
  164. {
  165.     if ( e->button() == LeftButton ) {
  166.     mousePressed = TRUE;
  167.     pressedAt = valueFromPosition( e->pos() );
  168.     }
  169. }
  170.  
  171. /*!
  172.   \reimp
  173. */
  174.  
  175. void QThumbWheel::mouseReleaseEvent( QMouseEvent *e )
  176. {
  177.     int movedTo = valueFromPosition( e->pos() );
  178.     setValue( value() + movedTo - pressedAt );
  179.     pressedAt = movedTo;
  180. }
  181.  
  182. /*!
  183.   \reimp
  184. */
  185.  
  186. void QThumbWheel::mouseMoveEvent( QMouseEvent *e )
  187. {
  188.     if ( !mousePressed )
  189.     return;
  190.     if ( track ) {
  191.     int movedTo = valueFromPosition( e->pos() );
  192.     setValue( value() + movedTo - pressedAt );
  193.     pressedAt = movedTo;
  194.     }
  195. }
  196.  
  197. /*!
  198.   \reimp
  199. */
  200.  
  201. void QThumbWheel::wheelEvent( QWheelEvent *e )
  202. {
  203.     int step = ( e->state() & ControlButton ) ? lineStep() : pageStep();
  204.     setValue( value() - e->delta()*step/120 );
  205.     e->accept();
  206. }
  207.  
  208. /*!\reimp
  209. */
  210.  
  211. void QThumbWheel::focusInEvent( QFocusEvent *e )
  212. {
  213.     QWidget::focusInEvent( e );
  214. }
  215.  
  216. /*!\reimp
  217. */
  218.  
  219. void QThumbWheel::focusOutEvent( QFocusEvent *e )
  220. {
  221.     QWidget::focusOutEvent( e );
  222. }
  223.  
  224. void QThumbWheel::drawContents( QPainter *p )
  225. {
  226.     QRect cr = contentsRect();
  227.     // double buffer
  228.     QPixmap pix( width(), height() );
  229.     QPainter pt( &pix );
  230.     QBrush brush = backgroundPixmap() ?
  231.         QBrush( backgroundColor(), *backgroundPixmap() ) : QBrush( backgroundColor() );
  232.     pt.fillRect( cr, brush );
  233.  
  234.     const int n = 17;
  235.     const double delta = m_pi / double(n);
  236.     // ### use positionFromValue() with rad*16 or similar
  237.     double alpha = 2*m_pi*double(value()-minValue())/
  238.            double(maxValue()-minValue())*transmissionRatio();
  239.     alpha = fmod(alpha, delta);
  240.     QPen pen0( colorGroup().midlight() );
  241.     QPen pen1( colorGroup().dark() );
  242.  
  243.     if ( orient == Horizontal ) {
  244.     double r = 0.5*cr.width();
  245.     int y0 = cr.y()+1;
  246.     int y1 = cr.bottom()-1;
  247.     for ( int i = 0; i < n; i++ ) {
  248.         int x = cr.x() + int((1-cos(delta*double(i)+alpha))*r);
  249.         pt.setPen( pen0 );
  250.         pt.drawLine( x, y0, x, y1 );
  251.         pt.setPen( pen1 );
  252.         pt.drawLine( x+1, y0, x+1, y1 );
  253.     }
  254.     } else {
  255.     // vertical orientation
  256.     double r = 0.5*cr.height();
  257.     int x0 = cr.x()+1;
  258.     int x1 = cr.right()-1;
  259.     for ( int i = 0; i < n; i++ ) {
  260.         int y = cr.y() + int((1-cos(delta*double(i)+alpha))*r);
  261.         pt.setPen( pen0 );
  262.         pt.drawLine( x0, y, x1, y );
  263.         pt.setPen( pen1 );
  264.         pt.drawLine( x0, y+1, x1, y+1 );
  265.     }
  266.     }
  267.     qDrawShadePanel( &pt, cr, colorGroup());
  268.  
  269.     pt.end();
  270.     p->drawPixmap( 0, 0, pix );
  271. }
  272.  
  273. int QThumbWheel::valueFromPosition( const QPoint &p )
  274. {
  275.     QRect wrec = contentsRect();
  276.     int pos, min, max;
  277.     if ( orient == Horizontal ) {
  278.     pos = p.x();
  279.     min = wrec.left();
  280.     max = wrec.right();
  281.     } else {
  282.     pos = p.y();
  283.     min = wrec.top();
  284.     max = wrec.bottom();
  285.     }
  286.     double alpha;
  287.     if ( pos < min )
  288.     alpha = 0;
  289.     else if ( pos > max )
  290.     alpha = m_pi;
  291.     else
  292.     alpha = acos( 1.0 - 2.0*double(pos-min)/double(max-min) );// ### taylor
  293.     double deg = alpha*rad_factor/transmissionRatio();
  294.     // ### use valueFromPosition()
  295.     return minValue() + int((maxValue()-minValue())*deg/360.0);
  296. }
  297.  
  298. #endif
  299.